package com.qsboss.demo.service.impl;

import cn.hutool.http.HttpRequest;
import com.qsboss.demo.domain.Result;
import com.qsboss.demo.service.SmsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.HashMap;

/**
 * @author 军哥
 * @version 1.0
 * @description: 短信发送
 * @date 2023/10/26 20:37
 */

@Service
@Slf4j
public class SmsServiceImpl implements SmsService {
    @Override
    public Result sendSms(String mobile, String code) {
// 定义发送短信网关地址以及授权编码
        String url = "https://gyytz.market.alicloudapi.com/sms/smsSend";
        String appcode = "bd7b0a53b94a43c5aa2ffecd39cb9619";
        String smsSignId = "2e65b1bb3d054466b82f0c9d125465e2";
        String templateId = "908e94ccf08b4476ba6c876d13f084ad";

        // 短信发送参数
        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("mobile", mobile);
        hashMap.put("param", "**code**:" + code + ",**minute**:5");
        hashMap.put("smsSignId", smsSignId);
        hashMap.put("templateId", templateId);

        // 发送短信
        String body = HttpRequest.post(url)
                .header("Authorization", "APPCODE " + appcode)
                .form(hashMap)
                .execute()
                .body();
        if (!body.contains("成功")){
            log.error("短信发送失败："+body);
            return Result.FAILED(500, "短信发送失败");
        }
        else {
            System.out.println(body);
        }
        return Result.SUCCESS();
    }
}
